package org.linlinjava.litemall.db.service;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.linlinjava.litemall.db.dao.PiccMessagePackageMapper;
import org.linlinjava.litemall.db.domain.PiccMessagePackage;
import org.linlinjava.litemall.db.domain.PiccMessagePackageExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PiccMessagePackageService {
    private final Log logger = LogFactory.getLog(PiccMessagePackageService.class);
    @Resource
    PiccMessagePackageMapper piccMessagePackageMapper;

    public PiccMessagePackage add(PiccMessagePackage msg){
        msg.setAddTime(LocalDateTime.now());
        piccMessagePackageMapper.insertSelective(msg);
        return msg;
    }

    /**
     * 直接返回护士和某一个病人的所有聊天记录
     * @param patientId
     * @param nurseId
     * @param sort
     * @param order
     * @return
     */
    public List<PiccMessagePackage> getMessagesByIds(Integer patientId, Integer nurseId,
                                                     String sort, String order){
        PiccMessagePackageExample example = new PiccMessagePackageExample();
        PiccMessagePackageExample.Criteria criteria = example.createCriteria();

        criteria.andPatientIdEqualTo(patientId);
        criteria.andNurseIdEqualTo(nurseId);
        example.setOrderByClause(sort + " " + order);

        return piccMessagePackageMapper.selectByExample(example);
    }

    /**
     * 查询护士和病人的聊天信息，根据已读和未读
     * @param patientId
     * @param nurseId
     * @param sort
     * @param order
     * @param status   false是未读，true是已读
     * @return
     */
    public List<PiccMessagePackage> getMessagesByStatus(Integer patientId, Integer nurseId, String role,
                                                        String sort, String order, boolean status){
        logger.debug(patientId+", "+nurseId+", "+role+", "+status);
        PiccMessagePackageExample example = new PiccMessagePackageExample();
        PiccMessagePackageExample.Criteria criteria = example.createCriteria();

        criteria.andPatientIdEqualTo(patientId);
        criteria.andNurseIdEqualTo(nurseId);
        criteria.andStatusEqualTo(status);
        //如果没有role的话，那就返回全部的消息
        //如果是病人查询，则将护士发送的作为未读；反之亦然
        if (role != null)
            criteria.andSenderNotEqualTo(role);

        example.setOrderByClause(sort + " " + order);
        List<PiccMessagePackage> mList = piccMessagePackageMapper.selectByExample(example);
        logger.debug(mList.toString());
        return mList;
    }

    public void updateById(PiccMessagePackage msg){
        piccMessagePackageMapper.updateByPrimaryKey(msg);
        return;
    }

    public int count(int nurseId) {
        PiccMessagePackageExample example = new PiccMessagePackageExample();
        if (nurseId > 0)
            example.or().andNurseIdEqualTo(nurseId);
        return (int) piccMessagePackageMapper.countByExample(example);
    }
}
